#include "report.h"

#define REGISTER "register"
#define INTO "into"
#define QUIT "quit"
#define OTA_INTO "OTAinto"

static void print_data_hex(uint8_t *data, uint8_t len)
{   
    printf("------> data:");
    for (int i = 0; i < len; i++) 
    {
        printf(" 0x%x", data[i]);
    }
    
    printf(", len1: %d\n", len);
}







/*注册回包*/
void pack_regiser_data()
{
    uint8_t mac[6];
    esp_err_t err = esp_read_mac(mac, ESP_MAC_WIFI_STA);
    if (ESP_OK == err)
    {
        char mac_str[18];
        snprintf(mac_str, sizeof(mac_str), "%02X:%02X:%02X:%02X:%02X:%02X",
        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        ESP_LOGI("MAC_INFO", "STA MAC Address: %s", mac_str);
    }
    else
    {
        ESP_LOGE("MAC_ERROR", "Failed to get STA MAC address. Error: %d", err);
    }
    

    


    u8 message[50] ={0x41,0x52,0x54,UART_RESISTER};
    memcpy(&message[6],(u8 *)REGISTER,8);
    /*版本号*/
    message[16] = ((PROD_FW_VER >>8)&0xFF);
    message[17] = (PROD_FW_VER & 0xFF);
    memcpy(&message[18],mac,6*sizeof(uint8_t));
    memcpy(&message[24],(u8*)PROD_NAME,strlen(PROD_NAME));

    /*总数据包字节长度*/
    u8 length = 4+2+8+2+2+strlen(PROD_NAME)+6;
    message[4] = ((length >>8)&0xFF);
    message[5] = (length & 0xFF);
    
    /*数据有效字节包*/
    u8 data_len =8+2+2+strlen(PROD_NAME)+6;
    message[14] =((data_len >>8)&0xFF);
    message[15] =(data_len & 0xFF);
    message[length] = (u8)END_MARKER;
    message[length+1] = (u8)END_MARKER;
    message[length+2] = (u8)END_MARKER;
    
    print_data_hex(message,length+3);
    uart_msg_send(message,length+3);
}

/*高速模式进入包*/
void pack_uart_into_data()
{
    u8 message[50] ={0x41,0x52,0x54,UART_INTO};
    memcpy(&message[6],(u8 *)INTO,4);
    /*总数据包*/
    u8 length = 4+2+strlen(INTO);
    message[4] = ((length >>8)&0xFF);
    message[5] = (length & 0xFF);

    message[length+1] = (u8)END_MARKER;
    message[length+2] = (u8)END_MARKER;
    message[length] = (u8)END_MARKER;
    print_data_hex(message,length+3);
    uart_msg_send(message,length+3);
}

/*高速模式数据回包*/
void pack_uart_return_data()
{
    u8 message[50] ={0x41,0x52,0x54,UART_RETURN};
    u8 length = 4+2;
    message[4] = ((length >>8)&0xFF);
    message[5] = (length & 0xFF);
    message[length+1] = (u8)END_MARKER;
    message[length+2] = (u8)END_MARKER;
    message[length] = (u8)END_MARKER;
    //print_data_hex(message,length+3);
    uart_msg_send(message,length+3);
}






/*高速模式退出包*/
void pack_uart_quit_data()
{
    u8 message[50] ={0x41,0x52,0x54,UART_QUIT};
    memcpy(&message[6],(u8 *)QUIT,4);
    /*总数据包*/
    u8 length = 4+2+strlen(QUIT);
    message[4] = ((length >>8)&0xFF);
    message[5] = (length & 0xFF);

    message[length+1] = (u8)END_MARKER;
    message[length+2] = (u8)END_MARKER;
    message[length] = (u8)END_MARKER;
    print_data_hex(message,length+3);
    uart_msg_send(message,length+3);
}

/*OTA进入包*/
void pack_uart_OTA_into_data()
{
    u8 message[50] ={0x41,0x52,0x54,UART_OTA_INTO};
    memcpy(&message[6],(u8 *)OTA_INTO,7);
    /*总数据包*/
    u8 length = 4+2+strlen(OTA_INTO);
    message[4] = ((length >>8)&0xFF);
    message[5] = (length & 0xFF);

    message[length+1] = (u8)END_MARKER;
    message[length+2] = (u8)END_MARKER;
    message[length] = (u8)END_MARKER;
    print_data_hex(message,length+3);

    uart_msg_send(message,length+3);
}











